package com.bawei.monthdemo2207a.month01.soft

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material3.Icon
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.bawei.monthdemo2207a.R

/**
 * 作者：赵亮
 * 时间：2024/12/26 16:05
 * 功能：软件页面
 */
@Composable
fun SoftPage() {

    val tabs = listOf("精选", "分类", "必备")
    var selectetTabIndex by remember {
        mutableStateOf(0)
    }

    Column(modifier = Modifier.padding(top = 24.dp)) {
        // 搜索标题
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 15.dp)
        ) {
            AndroidView(modifier = Modifier.weight(1f), factory = {
                val mySearch = MySearch(it)
                mySearch.mListener = {
                    Log.i("TAG", "SoftPage: ${it}")
                }
                mySearch
            })
            Icon(imageVector = Icons.Default.KeyboardArrowDown, contentDescription = null)

        }

        TabRow(
            modifier = Modifier
                .fillMaxWidth()
                .height(50.dp)
                .padding(horizontal = 15.dp), selectedTabIndex = selectetTabIndex
        ) {
            tabs.forEachIndexed { index, s ->
                Tab(
                    modifier = Modifier.fillMaxSize(),
                    selected = selectetTabIndex == index,
                    onClick = {
                        selectetTabIndex = index
                    }) {
                    Text(text = s)
                }
            }
        }

        when (selectetTabIndex) {
            0 -> {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 15.dp),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(text = "往期最佳")
                    Text(text = "更多 >")
                }
                LazyRow(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
                    items(100) {
                        Image(
                            modifier = Modifier
                                .size(270.dp, 160.dp)
                                .clip(RoundedCornerShape(15.dp)),
                            contentScale = ContentScale.Crop,
                            painter = painterResource(id = R.drawable.banner),
                            contentDescription = null
                        )
                    }
                }
            }

            1 -> {
                Box(modifier = Modifier.fillMaxSize()) {
                    Text(text = "分类", modifier = Modifier.align(Alignment.Center))
                    AndroidView(factory = {
                        MyView(it)
                    })
                }
            }

            2 -> {
                Box(modifier = Modifier.fillMaxSize()) {
                    Text(text = "必备", modifier = Modifier.align(Alignment.Center))
                }
            }
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun SoftPagePreview() {
    SoftPage()
}

